package com.dai.zhenfujin.visitingcard.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.Constants.EventConstants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.bean.MessageEvent;
import com.dai.zhenfujin.bean.ProInfoBean;
import com.dai.zhenfujin.bean.ProListBean;
import com.dai.zhenfujin.db.EaseConstant;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.message.ChatActivity;
import com.dai.zhenfujin.utils.SPUtils;
import com.dai.zhenfujin.utils.UIUtils;
import com.dai.zhenfujin.view.MyImageLoader;
import com.dai.zhenfujin.view.ShowImagesDialog;
import com.dai.zhenfujin.view.SodukuListView;
import com.dai.zhenfujin.visitingcard.app.adapter.PersonalDetailsListviewAdapter2;
import com.dai.zhenfujin.visitingcard.bean.ParticularsPageBean;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

/***
 * 产品的详情页面
 */
public class ParticularsPageActivity extends BaseActivity {

    private SodukuListView listView;

    private TextView tvTitleName, tvAddress, pdh_signature, pp_adapter_company, pd_colleague,tv_biaoti,item_word_job,
            tv_price, pp_adapter_name, pp_adapter_position, pp_adapter_bloc, attention, item_word_type;

    private FrameLayout imageView;
    private RelativeLayout rl_lahei, rl_yenzhen;
    private CircleImageView cv_img;
    private Button btSend;

    private List<ParticularsPageBean> beans;
   // private ParticularsPageAdapter adapter;
    private String pro_id, name,type_id;
    private Banner mBanner;
    private List<String> url_list = new ArrayList<>();
    private ArrayList<String> list_path = new ArrayList<>();
    private ArrayList<String> list_title = new ArrayList<>();
    private String user_id;
    private String user_mobile;
    private List<String> imgUrls;
    private String user_logo;
    private LinearLayout ll_msg;
    private PersonalDetailsListviewAdapter2 adapter;
    private List<ProListBean.ProListBean2> proListBean = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_particulars_page);
        pro_id = getIntent().getStringExtra("pro_id");
        type_id = getIntent().getStringExtra("type_id");

        initView();
        getProductInfo();


        tvTitleName.setText("产品详情");
        beans = new ArrayList<>();
        beans.add(new ParticularsPageBean("张艺兴", "总监", "张艺兴工作室集团", "张艺兴工作室",
                "在售 | 大户型 | 大阳台 ", "285万", "4室2厅2卫", "129平方", "12000元/平方",
                "南北", "中层(共7层)", "2018.05.26", "有", "普通住宅", "洪山-保利-保利时代",
                "这是一段房屋简介，这是一段房屋简介，这是一段房屋简介，这是一段房屋简介。" +
                        "这是一段房屋简介。是一段房屋简介这。是一段房屋简介这是一段房屋简介" +
                        "这是一段房屋简介这是一段房屋简介这是一段房屋简介这是一段房屋简介" +
                        "这是一段房屋简介这是一段房屋简介"
        ));

      //  adapter = new ParticularsPageAdapter(this, beans);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        //设置布局管理器
      //  recyclerView.setLayoutManager(linearLayoutManager);
        //设置为垂直布局，这也是默认的
       // linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //设置adapter
     //   recyclerView.setAdapter(adapter);



    }



    private void initView() {
        tvTitleName = findViewById(R.id.title_textview);
        mBanner = findViewById(R.id.pp_adapter_imageview); //轮播图
        imageView = findViewById(R.id.fl_img);
        tvAddress = findViewById(R.id.tv_address); // 地址
        pdh_signature = findViewById(R.id.pdh_signature); // 介绍
        pp_adapter_company = findViewById(R.id.pp_adapter_company); // 产品
        tv_price = findViewById(R.id.tv_price); // 价格
        cv_img = findViewById(R.id.cv_img); //
        pp_adapter_name = findViewById(R.id.pp_adapter_name); //
        pp_adapter_position = findViewById(R.id.pp_adapter_position); //
        pp_adapter_bloc = findViewById(R.id.pp_adapter_bloc); // 集团
        attention = findViewById(R.id.attention); // 关注
        item_word_type = findViewById(R.id.item_word_type);
        listView = findViewById(R.id.pp_recyclerview);
        btSend = findViewById(R.id.bt_send);
        rl_lahei = findViewById(R.id.rl_lahei); //
        rl_yenzhen = findViewById(R.id.rl_yenzhen); //
        pd_colleague = findViewById(R.id.pd_colleague); //同事
        tv_biaoti = findViewById(R.id.tv_biaoti); //标提
        item_word_job = findViewById(R.id.item_word_job); //副业

        ll_msg = findViewById(R.id.ll_msg);
        rl_lahei.setVisibility(View.GONE);
        rl_yenzhen.setVisibility(View.GONE);
        attention.setVisibility(View.GONE);

        adapter = new PersonalDetailsListviewAdapter2(this, proListBean);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    startActivity(new Intent(ParticularsPageActivity.this, ParticularsPageActivity.class)
                            .putExtra("pro_id", proListBean.get(position).getPro_id())
                            .putExtra("type_id", proListBean.get(position).type_id));
            }
        });

        pd_colleague.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ParticularsPageActivity.this, ColleagueActivity.class);
                intent.putExtra("company_name",pp_adapter_bloc.getText().toString().trim());
                intent.putExtra("user_id", user_id);
                startActivity(intent);
            }
        });
        // list_path.add("https://gss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/zhidao/wh%3D600%2C800/sign=e9873bfca944ad342eea8f81e09220cc/a8ec8a13632762d08fa73daea8ec08fa513dc602.jpg");

        //list_path.add("https://gss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/zhidao/wh%3D600%2C800/sign=e9873bfca944ad342eea8f81e09220cc/a8ec8a13632762d08fa73daea8ec08fa513dc602.jpg");

        //  list_path.add("https://gss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/zhidao/wh%3D600%2C800/sign=e9873bfca944ad342eea8f81e09220cc/a8ec8a13632762d08fa73daea8ec08fa513dc602.jpg");

        //  list_path.add("https://gss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/zhidao/wh%3D600%2C800/sign=e9873bfca944ad342eea8f81e09220cc/a8ec8a13632762d08fa73daea8ec08fa513dc602.jpg");
        list_title.add("我爱NBA");
        list_title.add("我爱科比布莱恩特");
        list_title.add("我爱NBA");
        list_title.add("我爱科比布莱恩特");


        //设置样式，里面有很多种样式可以自己都看看效果
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        mBanner.setImageLoader(new MyImageLoader());
        //设置轮播的动画效果,里面有很多种特效,可以都看看效果。
        mBanner.setBannerAnimation(Transformer.Default);
        //  mBanner.setBannerTitles(list_title);
        //设置轮播间隔时间
        mBanner.setDelayTime(3000);
        //设置是否为自动轮播，默认是true
        mBanner.isAutoPlay(true);
        //设置指示器的位置，小点点，居中显示
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        mBanner.setOnBannerListener(position -> {
            ShowImagesDialog showImagesDialog = new ShowImagesDialog(this, imgUrls, position);
            showImagesDialog.show();
        });

        cv_img.setOnClickListener(v -> {
            list_path.clear();
            list_path.add(user_logo);
            ShowImagesDialog showImagesDialog = new ShowImagesDialog(this, list_path, 0);
            showImagesDialog.show();
        });

        imageView.setOnClickListener(v -> finish());
        btSend.setOnClickListener(v -> {
            SPUtils.saveConversionPorId(this, user_mobile, pro_id);  //手机号码
            SPUtils.saveConversionUserId(this, user_mobile, user_id);  //手机号码
            SPUtils.saveSendMsg(this, user_mobile, true);
            if (user_mobile.equals(SPUtils.getString(ParticularsPageActivity.this, Constants.USER_MOBILE))) {
                UIUtils.showToastSafe("自己不能和自己聊天");
                return;
            }
            startActivity(new Intent(ParticularsPageActivity.this, ChatActivity.class).putExtra(EaseConstant.EXTRA_USER_ID, user_mobile));
        });


        //关注
        attention.setOnClickListener(v -> {
            if (user_mobile.equals(SPUtils.getString(ParticularsPageActivity.this, Constants.USER_MOBILE))) {
                UIUtils.showToastSafe("自己不能关注自己");
                return;
            }
            follow();
        });
    }


    private void getProductInfo() {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("pro_id", pro_id);
        params2.put("loc_x", SPUtils.getString(this, "longitude"));
        params2.put("loc_y", SPUtils.getString(this, "latitude"));
        params.put("req_bd", params2);
        HttpClient.post(this, Constants.PRODUCTINFO, params, new CallBack<ProInfoBean>() {
            @Override
            public void onSuccess(ProInfoBean proInfoBean) {
                getProductList();
                user_id = proInfoBean.getPro_info().getUser_id();
                pro_id = proInfoBean.getPro_info().getPro_id();
                pp_adapter_name.setText(proInfoBean.getPro_info().getUser_name());
                pp_adapter_position.setText(proInfoBean.getPro_info().getUser_duties());
                pp_adapter_bloc.setText(proInfoBean.getPro_info().getUser_company()); //公司
                item_word_type.setText("主业:" + proInfoBean.getPro_info().getUser_job()); //描述
                if (TextUtils.isEmpty(proInfoBean.getPro_info().getPro_name())){
                    tv_biaoti.setText("暂无标题"); //标题
                }else {
                    tv_biaoti.setText(proInfoBean.getPro_info().getPro_name()); //标题
                }

                if (TextUtils.isEmpty(proInfoBean.getPro_info().type_name)){
                    item_word_job.setText("暂无副业"); //副业
                }else {
                    item_word_job.setText(proInfoBean.getPro_info().type_name);
                }


                if (TextUtils.isEmpty(proInfoBean.getPro_info().getPro_address())){
                    tvAddress.setText("暂无地址");
                }else {
                    tvAddress.setText(proInfoBean.getPro_info().getPro_address());
                }

                if (TextUtils.isEmpty(proInfoBean.getPro_info().getPro_desc())){
                    pdh_signature.setText("暂无个性签名"); //介绍 个性签名
                }else {
                    pdh_signature.setText(proInfoBean.getPro_info().getPro_desc()); //介绍 个性签名
                }

                if (TextUtils.isEmpty(proInfoBean.getPro_info().getPro_price())) {
                    tv_price.setVisibility(View.INVISIBLE);
                } else {
                    tv_price.setVisibility(View.VISIBLE);
                    tv_price.setText("￥" + proInfoBean.getPro_info().getPro_price() + "元");
                }

              //  pp_adapter_company.setText(proInfoBean.getPro_info().getPro_name());

                if (proInfoBean.getPro_info().getIsfriend().equals("0")) {
                    attention.setText("未关注");
                } else {
                    attention.setText("已关注");
                }
                user_logo = proInfoBean.getPro_info().getUser_logo();
                Glide.with(ParticularsPageActivity.this).load(proInfoBean.getPro_info().getUser_logo()).into(cv_img);
                //设置图片加载地址
                imgUrls = proInfoBean.getPro_info().getUrl_list();
                mBanner.setImages(imgUrls).start();
                user_mobile = proInfoBean.getPro_info().user_mobile;
                //保存用户信息
                SPUtils.saveConversionHeadPhoto(ParticularsPageActivity.this, user_mobile, proInfoBean.getPro_info().getUser_logo());
                SPUtils.saveConversionNickName(ParticularsPageActivity.this, user_mobile, proInfoBean.getPro_info().getUser_name());
                SPUtils.saveConversionJob(ParticularsPageActivity.this, user_mobile, proInfoBean.getPro_info().getUser_job());

                if (user_mobile.equals(SPUtils.getString(ParticularsPageActivity.this, Constants.USER_MOBILE))) {
                    attention.setVisibility(View.GONE); //关注
                    ll_msg.setVisibility(View.GONE);
                    rl_yenzhen.setVisibility(View.GONE);
                }
            }
        });
    }

    private void getProductList() {
        Map<String, Object> params3 = new HashMap<>();
        Map<String, String> params4 = new HashMap<>();
        params4.put("type_id", type_id);
        params4.put("pro_id", pro_id);
        params4.put("words", "");
        params4.put("page", 1 + "");
        params4.put("number", 50 + "");
        params4.put("loc_x", SPUtils.getString(this, "longitude"));
        params4.put("loc_y", SPUtils.getString(this, "latitude"));
        params3.put("req_bd", params4);
        Log.i("getProductList",new Gson().toJson(params3));
        HttpClient.post(this, Constants.PRODUCTLIST, params3, new CallBack<ProListBean>() {
            @Override
            public void onSuccess(ProListBean proListBean2) {
                Log.i("getProductList",proListBean2.getProList().toString());
                proListBean.clear();
                proListBean.addAll(proListBean2.getProList());
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void follow() {
        int type2;
        if (attention.getText().toString().trim().equals("未关注")) {
            type2 = 1;
        } else {
            type2 = 2;
        }
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> params2 = new HashMap<>();
        params2.put("friend_id", user_id);
        params2.put("type", type2);
        params.put("req_bd", params2);
        HttpClient.post(this, Constants.FOLLOW, params, new CallBack<ProInfoBean>() {
            @Override
            public void onSuccess(ProInfoBean proInfoBean) {
                MessageEvent messageEvent = new MessageEvent();
                messageEvent.setMessageKey(EventConstants.UPDATA_FOLLOW_LIST);
                EventBus.getDefault().post(messageEvent);

                if (attention.getText().toString().equals("未关注")) {
                    attention.setText("已关注");
                } else {
                    attention.setText("未关注");
                }
            }
        });
    }
}
